﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using ARR.Entities;
using ARR.UI.StoresService;
using DDF.Entity;

namespace ARR.UI.Models
{
    public interface IStoresImporterModel : IModelBase
    {
        List<ImporterResult> Results { get; set; }

        ObservableCollection<TempStore> TempStores { get; set; }

        ObservableCollection<TempStore> TempStoresCache { get; set; }

        void Save();
    }

    public class StoresImporterModel : ModelBase, IStoresImporterModel
    {
        public StoresImporterModel()
        {
            this.Results = new List<ImporterResult>();
            this.TempStores = new ObservableCollection<TempStore>();
            this.TempStoresCache = new ObservableCollection<TempStore>();
        }


        public List<ImporterResult> Results { get; set; }

        public ObservableCollection<TempStore> TempStores { get; set; }

        public ObservableCollection<TempStore> TempStoresCache { get; set; }


        public void Save()
        {
            var toSave = TempStoresCache.Where(x => x.GetEntityState() != EntityState.Original);

            if (toSave.Any())
            {
                StoreServiceClient service = new StoreServiceClient();

                try
                {
                    service.SaveTempStores(toSave.ToList());
                    service.Close();
                }
                catch
                {
                    service.Abort();
                    throw;
                }

            }
        }
    }


    public class StoresImporterModelMock : MockModelBase, IStoresImporterModel
    {
        public StoresImporterModelMock()
        {
            this.Results = new List<ImporterResult>();
            this.TempStores = new ObservableCollection<TempStore>();
            this.TempStoresCache = new ObservableCollection<TempStore>();
        }


        public List<ImporterResult> Results { get; set; }

        public ObservableCollection<TempStore> TempStores { get; set; }

        public ObservableCollection<TempStore> TempStoresCache { get; set; }


        public void Save()
        {

        }
    }
}
